from flask import Blueprint, request
from models.book import Book
from flasgger import swag_from

book_blueprint = Blueprint('book_bp', __name__, url_prefix='/')


# GET方法/books获取所有图书信息
@book_blueprint.route('/books', methods=['GET'])
@swag_from('E:/Py_code/book/yml/book.yml')
def get_books():
    return Book.get_book()


# POST方法/books添加新图书
@book_blueprint.route('/books', methods=['POST'])
@swag_from('E:/Py_code/book/yml/book.yml')
def add_book():
    data = request.get_json()  # 解析JSON数据
    # 从JSON数据中提取所需的字段
    title = data.get('title')
    author = data.get('author')
    isbn = data.get('isbn')
    return Book.add_book(title, author, isbn)


# PUT方法/books/<bookId>更新图书信息
@book_blueprint.route('/books/<bookId>', methods=['PUT'])
@swag_from('E:/Py_code/book/yml/book.yml')
def update_book(bookId):
    data = request.get_json()  # 解析JSON数据
    # 从JSON数据中提取所需的字段
    title = data.get('title')
    author = data.get('author')
    isbn = data.get('isbn')
    available = data.get('available')
    return Book.update_book(bookId, title, author, isbn, available)


# DELETE方法/books/<bookId>删除图书
@book_blueprint.route('/books/<bookId>', methods=['DELETE'])
@swag_from('E:/Py_code/book/yml/book.yml')
def delete_book(bookId):
    return Book.delete_book(bookId)

